<?php

namespace app\api\model\service;

use think\Exception;
use think\Model;
class Withdraw extends Model
{
    // 表名
    protected $name = 'service_withdraw';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    public static function apply($params)
    {
        //方式,金额,信息,提现日期
        $userInfo = UserInfo::getUserMoney($params['user_id']);
        self::compareNum(['type'=>$params['type'],'num'=>$params['num']],$userInfo);
        self::compareWithdrawType($params['withdrawtype'],$userInfo);
        //平台还是商户
        if($params['type'] == 1){
            //找商户
            $params['shop_id'] = self::shopWithdrawDay($params['user_id']);
        }
        switch ($params['withdrawtype'])
        {
            case 0:
                $params['withdraw_info'] = json_encode('支付宝账号:'.$userInfo['alipay_account'].',支付宝姓名:'.$userInfo['alipay_name'],JSON_UNESCAPED_UNICODE);
                break;
            case 1:
                $params['withdraw_info'] = json_encode('开户行:'.$userInfo['bank_name'].',开户人:'.$userInfo['bank_user'].',银行卡号:'.$userInfo['bank_card'],JSON_UNESCAPED_UNICODE);
                break;
        }
        (new Withdraw)->allowField(true)->data($params)->save();
        switch ($params['type'])
        {
            case 0:
                UserInfo::money(-$params['num'],$params['user_id'],'平台余额提现扣除');
                break;
            case 1:
                UserInfo::shopMoney(-$params['num'],$params['user_id'],'商户余额提现扣除');
                break;
            case 2:
                UserInfo::shopUserMoney(-$params['num'],$params['user_id'],'商户余额提现扣除');
                break;
            case 3:
                Skill::money(-$params['num'],$params['user_id'],'保证金提现扣除');
                $skill = Skill::where('user_id', $params['user_id'])->field('user_id,id,ensure_price')->find();
                SkillEnsurePay::reduceSkillEnsure($skill['id']);
                break;
            case 4:
                Shop::money(-$params['num'],$params['user_id'],'保证金提现扣除');
                $shop = Shop::where('user_id', $params['user_id'])->field('user_id,id,ensure_price')->find();
                ShopEnsurePay::reduceShopEnsure($shop['id']);
                break;
        }
        return true;
    }

    public static function platformWithdrawDay()
    {
        $config = ProjectConfig::where(['state'=>1])->field('withdrawtype,day')->find();
        if(($config['withdrawtype'] == 0 && date("d") != $config['day']) || ($config['withdrawtype'] == 1 && date('w') != $config['day']))
        {
            throw new Exception('未到提现日期');
        }
        return true;
    }


    public static function shopWithdrawDay($uid)
    {
        $shopId = Skill::where(['user_id'=>$uid])->value('shop_id');
        $shop = Shop::where(['id'=>$shopId])->field('withdrawtype,day')->find();
        if(!$shop)
        {
            throw new Exception('所属商户信息异常,请联系管理员');
        }
        if(($shop['withdrawtype'] == 0 && date("d") != $shop['day']) || ($shop['withdrawtype'] == 1 && date('w') != $shop['day']))
        {
            throw new Exception('未到提现日期');
        }
        return $shopId;
    }


    public static function compareWithdrawType($type,$userInfo)
    {
        if(($type == 0 && (!$userInfo['alipay_account'] || !$userInfo['alipay_name'])) || ($type == 1 && (!$userInfo['bank_name'] || !$userInfo['bank_user'] || !$userInfo['bank_card'])))
        {
            throw new Exception('请完善提现账户信息');
        }
        return true;
    }

    public static function compareNum($params,$userInfo)
    {

        if(($params['type'] == 0 && $userInfo['money']<$params['num']) || ($params['type'] == 1 && $userInfo['shop_money']<$params['num']) || ($params['type'] == 2 && $userInfo['shop_user_money']<$params['num']) || ($params['type'] == 3 && $userInfo['skillEnsurePrice']<$params['num']) || ($params['type'] == 4 && $userInfo['shopEnsurePrice']<$params['num']))
        {
            throw new Exception('提现金额大于当前金额,请重新填写');
        }
        return true;
    }

    public static function getList($params)
    {
        extract($params);
        $where = [];
        if(isset($user_id) && $user_id != '')
        {
            $where['user_id'] = $user_id;
        }
        if(isset($shop_id) && $shop_id != '')
        {
            $where['shop_id'] = $shop_id;
        }
        if(isset($withdrawtype) && $withdrawtype != '')
        {
            $where['withdrawtype'] = $withdrawtype;
        }
        if(isset($state) && is_numeric($state))
        {
            $where['state'] = $state;
        }
        if(isset($type) && is_numeric($type))
        {
            $where['type'] = $type;
        }
        $list = self::where($where)->order('id desc')->page($page)->limit(10)->select();
        foreach ($list as $key=>$value)
        {
            $list[$key]['withdraw_info'] = json_decode($value['withdraw_info'],true);
            $list[$key]['images'] = $value['images']?explode(',',$value['images']):'';
        }
        return $list;
    }

}
